package com.demo.server;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

import com.demo.data.*;
public class Server 
{
	private ServerSocket serverSocket;
	private Socket clientSocket;
	
	public Server()
	{
		try
		{			
			if(serverSocket == null)
			{
				serverSocket = new ServerSocket(Data.PORT);	
				System.out.println("Server is running");
			}
		}		
		catch (NumberFormatException e) 
		{
			System.out.println("Wrong port!");
		} 
		catch (IllegalArgumentException e) 
		{
			System.out.println("Port in out of (0 - 65535)!");
		} 
		catch (BindException e) 
		{
			System.out.println("Port already use!");
		}
		catch(IOException e)
		{
			System.out.println("IO error!");
		}
	}		
	
	public void startServer() 
	{
		try
		{
			while(true)
			{
				clientSocket = serverSocket.accept();
				new ServerThread(clientSocket).start();
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		
	}
	public void stopServer() 
	{
		
	}
}
